package com.nextbus.ui;

import java.util.List;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout.LayoutParams;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.nextbus.main.R;

/**
 * @author dxt
 *
 * @param <Item>
 */
public abstract class BalloonItemizedOverlay <Item extends OverlayItem> extends ItemizedOverlay<Item>{
	
	private MapView mapView;
	protected BalloonoverLayView<Item> balloonView;
	private View clickRegion;
	private int viewOffset;
	protected Item currentFocussedItem;
	protected int currentFocussedIndex;
	public BalloonItemizedOverlay(Drawable defaultMarker, MapView mapView) {
		super(defaultMarker);
		this.mapView = mapView;
	}
	

	public BalloonItemizedOverlay(Drawable defaultMarker) {
		super(defaultMarker);
	}
	
	protected abstract Item createItem(int i); 
	
	protected boolean onTap(int index) {		
		System.out.println("当前选择的index是：    "+index);
		currentFocussedIndex = index;
		currentFocussedItem = createItem(index);
		
		boolean isRecycled;
		if (balloonView == null) {
			
			balloonView = createBalloonOverlayView();
			
			clickRegion = (View) balloonView.findViewById(R.id.balloon_main_layout);
			clickRegion.setOnTouchListener(createBalloonTouchListener());
			isRecycled = false;
		} else {
			isRecycled = true;
		}
	
		balloonView.setVisibility(View.GONE);
		
		List<Overlay> mapOverlays = mapView.getOverlays();
		if (mapOverlays.size() > 1) {
			hideOtherBalloons(mapOverlays);
		}
		
		balloonView.setData(currentFocussedItem);
		
		GeoPoint point = currentFocussedItem.getPoint();
		MapView.LayoutParams params = new MapView.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, point,
				MapView.LayoutParams.BOTTOM_CENTER);
		params.mode = MapView.LayoutParams.MODE_MAP;
		
		balloonView.setVisibility(View.VISIBLE);

		if (isRecycled) {
			balloonView.setLayoutParams(params);
		} else {
			mapView.addView(balloonView, params);
		}
		return true;
	}
	
	private void hideOtherBalloons(List<Overlay> overlays) {
		
		for (Overlay overlay : overlays) {
			if (overlay instanceof BalloonItemizedOverlay<?> && overlay != this) {
				((BalloonItemizedOverlay<?>) overlay).hideBalloon();
			}
		}
		
	}
	
	/**
	 * 创建弹出框视图实体，这是一个虚函数，会在busoverlay里面实际调用的
	 * can populate additional sub-views.
	 */
	protected abstract BalloonoverLayView<Item> createBalloonOverlayView();

	//设置弹出框的偏移量
	public void setBalloonBottomOffset(int pixels) {
		viewOffset = pixels;
	}
	public int getBalloonBottomOffset() {
		return viewOffset;
	}
	
	protected MapView getMapView() {
		return mapView;
	}
	
	@Override
	public int size() {
		// TODO Auto-generated method stub
		return 0;
	}
	
	/**
	 * 隐藏弹出框，使其不可见
	 */
	protected void hideBalloon() {
		if (balloonView != null) {
			balloonView.setVisibility(View.GONE);
		}
	}
	
	protected boolean onBalloonTap(int index, Item item) {
		return false;
	}
	
	/**
	 * Sets the onTouchListener for the balloon being displayed, calling the
	 * overridden {@link #onBalloonTap} method.
	 */
	private OnTouchListener createBalloonTouchListener() {
		return new OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				
				View l =  ((View) v.getParent()).findViewById(R.id.balloon_main_layout);
				Drawable d = l.getBackground();
				
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					int[] states = {android.R.attr.state_pressed};
					if (d.setState(states)) {
						d.invalidateSelf();
					}
					
				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					int newStates[] = {};
					if (d.setState(newStates)) {
						d.invalidateSelf();
					}
					// call overridden method
					onBalloonTap(currentFocussedIndex, currentFocussedItem);
				}
				
				return false;
			}
		};
	}
	
}
	

